/**
 * Created by DELL on 2017/7/5.
 */
common.buildNormalEnumSelect(".bannerType", enumObject.enumJsonData['bannerType']);//创建 banner类型 枚举列表
common.buildNormalEnumSelect(".dataStatus", enumObject.enumJsonData['dataStatus']);//创建 使用状态 枚举列表
var regImg=/(\.jpg|\.jpeg|\.gif|\.png)$/;
var checkedObject = {};
var settings = {
    "sAjaxSource": window.globalHost + common.bannerListPath,
    "aoColumns": [{
        "mDataProp": function(obj) {
            var checkboxItemStr = '<input id="checkbox-' + obj.id + '" data-name="'+obj.contactName+'" type="checkbox" >';
            if (checkedObject[obj.id]) {
                checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" checked="checked">';
            }
            return '<div class="checkbox checkbox-danger" style="width:100%">' + checkboxItemStr + '<label for="checkbox-' + obj.id + '" style="width:80%"></label></div>'
        },
        "sTitle": "",
        "sDefaultContent": "",
        "sClass": "center"
    },{
        "mDataProp": function (obj) {
            return regImg.test(obj.picturePath)?'<img src="'+window.fileHost+obj.picturePath+'">':"图片路径不合法";
        },
        "sTitle": "图片",
        "sDefaultContent": "",
        "sClass": "text-center banner-image"
    }, {
        "mDataProp": function (obj) {
            return '<a href="javascript:;" class="link" data-id="' + obj.id + '" >'+(!!obj.linkAddress?obj.linkAddress:"无信息")+'</a>';
        },
        "sTitle": "链接地址",
        "sDefaultContent": ""
    }, {
        "mDataProp": 'createTime',
        "sTitle": "创建时间",
        "sDefaultContent": ""
    }, {
        "mDataProp": "updateTime",
        "sTitle": "更新时间",
        "sDefaultContent": ""
    }, {
        "mDataProp":function (obj) {
            return obj.sort==99?"":obj.sort;
        },
        "sTitle": "序列号",
        "sDefaultContent": ""
    },{
        "mDataProp": function (obj) {
            return enumObject.enumJsonData['bannerType'][obj.bannerType];
        },
        "sTitle": "banner类型",
        "sDefaultContent": ""
    },{
        "mDataProp": function (obj) {
            return enumObject.enumJsonData['dataStatus'][obj.status];
        },
        "sTitle": "使用状态",
        "sDefaultContent": ""
    }, {
        "mDataProp": function (obj) {
            var btn='';
            var flag=obj.status=='AVAILABLE';
            if(obj.status=='AVAILABLE'){
                btn='<a href="javascript:;" class="btn btn-danger status" data-id="'+obj.id+'" data-target="'+"FORBID"+'">禁用</a>';
            }else{
                btn='<a href="javascript:;" class="btn btn-info status" data-id="'+obj.id+'" data-target="'+"AVAILABLE"+'">启用</a>';
            }
            return btn;
        },
        "sTitle": "操作",
        "sDefaultContent": "",
        sClass: "clearfix text-center"
    }],
    "fnServerParams": function (aoData) {
        return common.getSearchData("form.data-list-search", aoData);
    }
};
var options = $.extend(true, {}, _dataTableSettings, settings);
/**
 * 实例化 中介联系人 列表插件
 * @type {[type]}
 */
var $currentDataTable = $("#data-list");
var oTable = $currentDataTable.dataTable(options);
searchList.getDataList(oTable);
searchList.resetSearch(oTable);

$currentDataTable.off("click", ".banner-image").on("click", ".banner-image", function (e) {
    var $this=$(this);
    var selfImg=$this.find("img");
    var selfSrc=selfImg.attr("src");
    var content='<p style="margin:0;line-height: 600px;"><img style="width:100%;vertical-align: middle;" src="'+selfSrc+'" alt=""></p>';
    layer.open({
        type:1,
        title:false,
        shadeClose:true,
        content:content,
        area:['600px','600px']
    });
    e.stopPropagation();
});

var userInfo = common.getLocalData(common.agentSysCacheWord).user;
$("button.insertBanner").on('click', function (ev) {
    common.openInsertDialog({
        'url': './webConf/insertBanner.html',
        'title': '添加banner',
        'sAjaxSource': window.globalHost + common.addBannerPath,
        'area': ['1000px', '600px'],
        ok: function (data) {
            //  新增信息成功之后，刷新列表页面
            oTable.fnClearTable();
        },
        btn: ['保存', '取消'],
        cancel: function () {
        }
    });
});
$currentDataTable.off("click", ".link").on("click", ".link", function (e) {
    var bannerId = $(this).attr("data-id");
    common.openInsertDialog({
        'url': './webConf/bannerInfo.html?id='+bannerId,
        'title': 'banner信息',
        'sAjaxSource': window.globalHost + common.updateBannerPath,
        'area': ['1000px', '600px'],
        ok: function (data) {
            //  新增信息成功之后，刷新列表页面
            oTable.fnClearTable();
        },
        btn: ['保存', '取消'],
        cancel: function () {
        }
    });
});
$currentDataTable.off("click", ".status").on("click", ".status", function (e) {
    var bannerId = $(this).attr("data-id");
    var target = $(this).attr("data-target");
    var sendData={id:bannerId,status:target};
    var confirmMsg=target=="FORBID"?"确定禁用该banner":"确定启用该banner";
    common.updateLock({
        confirmMsg: confirmMsg,
        path: common.updateBannerPath,
        sendData: sendData,
        listPanel:oTable
    });
});

$currentDataTable.off("change",".checkbox").on("change", ".checkbox", function(ev) {
    var checkItem = $(this).find("input[type='checkbox']")[0];
    var checkId = checkItem.id.replace('checkbox-', '');
    if (checkItem.checked) {
        checkedObject[checkId] = true;
    } else {
        delete checkedObject[checkId];
    }
    ev.stopPropagation();
});

$("button.delBanner").on("click", function(ev) {
    common.changeConfirm("确认删除此类banner ？",batchDelete);
    ev.stopPropagation();
});
/**
 * 批量删除方法
 * @param  {[type]} data [description]
 * @return {[type]}      [description]
 */
function batchDelete() {
    var ids = [];
    if($.isEmptyObject(checkedObject)){
        layer.alert("请选择要删除的banner!!");
        return;
    }
    $.each(checkedObject, function(key, val) {
        ids.push(key);
    });
    ids = ids.join();
    $.post(window.globalHost + common.delBannerPath, { ids: ids }).success(function(data) {
        var record = common.transformData(data);
        layer.alert(record.res.msg);
        /**
         * 删除成功之后，清除本地的checkbox缓存。
         * @type {Object}
         */
        checkedObject = {};
        /**
         * 批量删除成功之后，刷新列表
         */
        oTable.fnClearTable();
    });
}